package com.yc.cloud.basic.utils;

import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;


@Slf4j
public class SystemInfoUtils {

    public static double getCpuUsage() {
        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
        return osBean.getSystemLoadAverage();
    }

    public static long getTotalMemory() {
        MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
        return memBean.getHeapMemoryUsage().getMax();
    }

    public static long getUsedMemory() {
        MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
        return memBean.getHeapMemoryUsage().getUsed();
    }

    public static long getTotalDiskSize() {
        try {
            File file = new File("/");
            FileStore fileStore = null;
            fileStore = FileSystems.getDefault().provider().getFileStore(file.toPath());
            return fileStore.getTotalSpace();
        } catch (IOException e) {

        }
        return -1L;
    }

    public static long getFreeDiskSize(String path) {
        try {
            File file = new File(path);
            FileStore fileStore = null;
            fileStore = FileSystems.getDefault().provider().getFileStore(file.toPath());
            return fileStore.getUsableSpace();
        } catch (IOException e) {

        }
        return -1L;
    }


    public static String getMacAddress() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                byte[] macBytes = networkInterface.getHardwareAddress();
                if (macBytes != null) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < macBytes.length; i++) {
                        sb.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? ":" : ""));
                    }
                    System.out.println("Interface: " + networkInterface.getName() + ", MAC Address: " + sb.toString());
                    return sb.toString();
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) throws InterruptedException {
        while (true) {
            val cpu = getCpuUsage();
            val memory = MyFileUtils.convertFileSize(getTotalMemory());
            val diskSize = MyFileUtils.convertFileSize(getFreeDiskSize("/"));
            log.info("cpu:[{}],memory:[{}],diskSize:[{}]", cpu, memory, diskSize);
            TimeUnit.SECONDS.sleep(1L);
        }

    }
}
